import { HTTP } from '@/utils/httpRequestUtil'
import URL from './api'
export default {
  uploadFile (fileObj, cb) {
    const formData = new FormData()
    formData.append('file', fileObj.file)
    formData.append('type', fileObj.file.type)
    return HTTP.request({
      url: URL.uploadFile,
      method: 'post',
      data: formData,
      // onUploadProgress: progressEvent => {
      //   let complete = (progressEvent.loaded / progressEvent.total * 100 | 0)
      //   if (complete === 100) {
      //     complete = 99
      //   }
      //   fileObj.onProgress({ percent: complete })
      // }
    }).then(function (response) {
      cb(response)
      // fileObj.onProgress({ percent: 100 })
    }).catch(function (error) {
      cb(error.response)
    })
  },
  sheUploadFile (fileObj, cb) {
    const formData = new FormData()
    formData.append(fileObj.filename, fileObj.file)
    for (let k in fileObj.param) { // 设置参数
      formData.append(`${k}`, fileObj.param[`${k}`])
    }
    return HTTP.request({
      url: URL.sheUploadFile,
      headers: {
        'enctype': 'multipart/form-data',
        'custom-printDebugInfo': 'Y'
      },
      method: 'post',
      data: formData,
    }).then(function (response) {
      cb(response.data)
      // fileObj.onProgress({ percent: 100 })
    }).catch(function (error) {
      cb(error.response)
    })
  },
  downloadFile (file, cb) {
    HTTP.request({
      url: '/file/downloadFile',
      method: 'post',
      // responseType: 'arraybuffer'
      headers: {
        'Content-Type': 'application/json; application/octet-stream'
      },
      responseType: 'blob',
      data: file
    }).then(function (response) {
      cb(response)
      // fileObj.onProgress({ percent: 100 })
    }).catch(function (error) {
      cb(error.response)
    })
  }
}
